package com.mephi.oshs.view.impl;

import com.mephi.oshs.view.api.IExploreView;
import com.mephi.oshs.view.api.ViewId;
import com.mephi.oshs.view.elements.*;
import com.mephi.oshs.view.lang.Labels;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.*;

import static com.mephi.oshs.view.impl.GridLayoutManager.getGridLayout;
import static com.mephi.oshs.view.impl.GridLayoutManager.getLayoutDataFillHorizontal;

public class ExploreView extends AbstractView implements IExploreView {

    private IAttributesTable table;
    private Textable shortNameText;
    private Clickable saveEdits;

    @Override
    public void createGUI() {
        this.parent.setLayout(getGridLayout());

        final Label shortNameLabel = new Label(parent, SWT.NONE);
        shortNameLabel.setLayoutData(getLayoutDataFillHorizontal());
        shortNameLabel.setText(TEXT_MANAGER.getText(Labels.EXPLORE_VIEW_SHORT_NAME));
        shortNameLabel.setAlignment(SWT.CENTER);

        final Text shortNameText = new Text(parent, SWT.BORDER);
        shortNameText.setLayoutData(getLayoutDataFillHorizontal());

        this.shortNameText = new Textable(shortNameText);

        this.table = new AttributesTable(this.createTable());

        final Button saveEdits = new Button(parent, SWT.CENTER);
        saveEdits.setLayoutData(getLayoutDataFillHorizontal());
        saveEdits.setText(TEXT_MANAGER.getText(Labels.EXPLORE_VIEW_SAVE_ACTION));

        this.saveEdits = new Clickable(saveEdits);
    }

    private Table createTable() {
        final Composite composite = new Composite(parent, SWT.NONE);

        final Table table = new Table(composite, SWT.VIRTUAL | SWT.BORDER | SWT.FULL_SELECTION);
        table.setHeaderVisible(true);
        table.setLinesVisible(true);
        //table.setItemCount(100 * 1000);

        final TableColumn[] tableColumns = new TableColumn[3];
        for (int i = 0; i < tableColumns.length; i++) {
            tableColumns[i] = new TableColumn(table, SWT.NONE);
        }
        tableColumns[0].setText(TEXT_MANAGER.getText(Labels.EXPLORE_VIEW_ATTR_NAME));
        tableColumns[1].setText(TEXT_MANAGER.getText(Labels.EXPLORE_VIEW_ATTR_TYPE));
        tableColumns[2].setText(TEXT_MANAGER.getText(Labels.EXPLORE_VIEW_ATTR_VALUE));

        /*
        for (final TableColumn column : tableColumns) {
            column.pack();
        }*/

        composite.addControlListener(new ResizeColumns(composite, table, tableColumns));

        composite.setLayoutData(new GridData(GridData.FILL_BOTH));
        return table;
    }

    @Override
    public ITextable getShortNameTextField() {
        return this.shortNameText;
    }

    @Override
    public IClickable getSaveEditsAction() {
        return this.saveEdits;
    }

    @Override
    public ViewId getViewID() {
        return ID;
    }

    @Override
    public IAttributesTable getAttributesTable() {
        return table;
    }

    private class ResizeColumns extends ControlAdapter {
        private final Composite composite;
        private final Table table;
        private final TableColumn[] columns;

        private ResizeColumns(final Composite composite, final Table table, final TableColumn[] columns) {
            this.composite = composite;
            this.table = table;
            this.columns = columns;
        }

        @Override
        public void controlResized(ControlEvent e) {
            final Rectangle area = composite.getClientArea();
            final Point preferredSize = table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
            int width = area.width - 2 * table.getBorderWidth();
            if (preferredSize.y > area.height + table.getHeaderHeight()) {

                // Subtract the scrollbar width from the total column width
                // if a vertical scrollbar will be required
                final Point vBarSize = table.getVerticalBar().getSize();
                width -= vBarSize.x;
            }
            final Point oldSize = table.getSize();
            final int columnWidth = width / (columns.length);
            if (oldSize.x > area.width) {

                // table is getting smaller so make the columns
                // smaller first and then resize the table to
                // match the client area width
                for (final TableColumn column : columns) {
                    column.setWidth(columnWidth);
                }
                table.setSize(area.width, area.height);
            } else {

                // table is getting bigger so make the table
                // bigger first and then make the columns wider
                // to match the client area width
                table.setSize(area.width, area.height);
                for (final TableColumn column : columns) {
                    column.setWidth(columnWidth);
                }
            }
        }
    }
}
